﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Base.DAL;
using Base.BLL;
using Base.Util;
using System.Data;
using Ay.Framework.WPF;
using MPSystem.Models;
using ADX.Views;

namespace MPSystem.Views.YFQ
{
    /// <summary>
    /// DP_test_Condition4.xaml 的交互逻辑 综采工作阻力历史数据分析曲线查询
    /// </summary>
    public partial class ZCYLPage_History_Condition : Page
    {
        public Dictionary<string, string> Cond = new Dictionary<string, string>();
        public static ZCYLPage_History_Condition _Me = null;
        public static ZCYLPage_History_Condition Me
        {
            get
            {
                if (_Me == null)
                {
                    _Me = new ZCYLPage_History_Condition();
                    _Me.UpdateCond();
                }
                return _Me;
            }
            set { _Me = value; }
        }
        int gzmbh = 0;

        public ZCYLPage_History_Condition()
        {
            InitializeComponent();
            LoadData();
            Me = this;
        }

        public void LoadData()
        {
            #region 加载单位
            string strP = "MPa,KN";
            string[] arP = strP.Split(',');
            foreach (var ar in arP)
            {
                CB_Dw.Items.Add(ar);
            }
            CB_Dw.SelectedIndex = 0;
            #endregion


            LoadBH();

            CB1.IsEnabled = false;
            CB2.IsEnabled = false;
            CB3.IsEnabled = false;
            RQ1.IsChecked = true;
            Begtime.SelectedDate = DateTime.Now;
            Endtime.SelectedDate = DateTime.Now;
        }

        #region 读取压力分站编号
        private bool isFirstLoad = true;
        public void LoadBH()
        {
            if (isFirstLoad || gzmbh != ZCYLPage_History.gzmbh)
            {
                RQ1_Click(this, null);
                isFirstLoad = false;
                gzmbh = ZCYLPage_History.gzmbh;
            }
        }
        #endregion

        public void UpdateCond()
        {
            Cond.Clear();
            try
            {
                if (CB_WZ.SelectedIndex < 0)
                {
                    AyMessageBox.Show("请选编号或位置");
                    return;
                }
                if (CB_Dw.SelectedIndex < 0)
                {
                    AyMessageBox.Show("请选单位");
                    return;
                }
                //状态标志位
                int s = 0, s1 = 1, s2 = 2, s3 = 4, s4 = 8, s5 = 16;
                //单柱选择情况,0代表整柱，不为0时，其二进制从低到高位分别代表左、右、不使用
                string SFZJ = "0";
                if (RQ4.IsChecked == true)
                {
                    if (CB1.IsChecked == true) s = s | s1;
                    if (CB2.IsChecked == true) s = s | s2;
                    if (CB3.IsChecked == true) s = s | s3;
                    SFZJ = s.ToString();
                }
                //加权平均选择情况
                //其二进制从低到高位分别代表2、4、8、12、24小时加权平均数
                s = 0;
                if (twoH.IsChecked == true) s = s | s1;
                if (eightH.IsChecked == true) s = s | s3;
                if (DayH.IsChecked == true) s = s | s5;
                string hSpan = s.ToString();
                //是否需要计算均值和方差
                //其二进制从低到高位分别代表均值、均值+方差、均值+2倍方差
                s = 0;
                if (P.IsChecked == true) s = s | s1;
                if (P1.IsChecked == true) s = s | s2;
                if (P2.IsChecked == true) s = s | s3;
                string stat = s.ToString();
                if (RQ1.IsChecked == true)
                {
                    Cond.Add("bh", CB_WZ.SelectedValue.ToString());
                    Cond.Add("wz", "");
                }
                else
                {
                    Cond.Add("bh", "0");
                    Cond.Add("wz", CB_WZ.SelectedValue.ToString());
                }
                Cond.Add("sfzj", SFZJ);
                Cond.Add("dtStart", Begtime.Text.ToString());
                Cond.Add("dtEnd", Endtime.Text.ToString());
                Cond.Add("hSpan", hSpan);
                Cond.Add("MPa", CB_Dw.SelectedValue.ToString());
                Cond.Add("stat", stat);
            }
            catch (Exception ex)
            {
                LoggerHelper.Error(ex);
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.CloseParentWindow();

        }
        
        #region 按键控制事件
        private void RQ1_Click(object sender, RoutedEventArgs e)
        {
            #region 加载P1、P2、P3名称
            var cs = ConfigService.FindZc(ZCYLPage_History.cqbh, ZCYLPage_History.gzmbh);
            CB1.Content = cs.FIRSTTD;
            CB2.Content = cs.SECONDTD;
            CB3.Content = cs.THIRDTD;
            #endregion

            if (RQ1.IsChecked == true)
            {
                P.IsEnabled = true;
                P1.IsEnabled = true;
                P2.IsEnabled = true;
                var lWz = ConfigService.GetZcWz(ZCYLPage_History.gzmbh);
                DataTable tempTable = new DataTable();
                DataColumn dc1 = new DataColumn("bh", Type.GetType("System.Int32"));
                DataColumn dc2 = new DataColumn("mc", Type.GetType("System.String"));
                tempTable.Columns.Add(dc1);
                tempTable.Columns.Add(dc2);
                //CB_WZ.ItemsSource = null;
                //CB_WZ.ItemsSource = tempTable.DefaultView;
                foreach (var r in lWz)
                {
                    DataRow dr0 = tempTable.NewRow();
                    dr0["bh"] = r.YLFJBH;
                    dr0["mc"] = r.YLFJBH;
                    tempTable.Rows.Add(dr0);
                }
                tempTable.AcceptChanges();
                CB_WZ.ItemsSource = tempTable.DefaultView;
                if(lWz.Count()>0)
                    CB_WZ.SelectedIndex = 0;
                tempTable.Dispose();
            }
        }

        private void RQ2_Click(object sender, RoutedEventArgs e)
        {
            if (RQ2.IsChecked == true)
            {
                P.IsEnabled = false;
                P1.IsEnabled = false;
                P2.IsEnabled = false;
                P.IsChecked = false;
                P1.IsChecked = false;
                P2.IsChecked = false;
                twoH.IsChecked = true;
                eightH.IsChecked = true;
                DayH.IsChecked = true;

                string lWz = "全部,上部,中部,下部";
                string[] arrWz = lWz.Split(',');
                DataTable tempTable = new DataTable();
                DataColumn dc1 = new DataColumn("bh", Type.GetType("System.String"));
                DataColumn dc2 = new DataColumn("mc", Type.GetType("System.String"));
                tempTable.Columns.Add(dc1);
                tempTable.Columns.Add(dc2);
                //CB_WZ.ItemsSource = null;
                //CB_WZ.ItemsSource = tempTable.DefaultView;
                foreach (var r in arrWz)
                {
                    DataRow dr0 = tempTable.NewRow();
                    dr0["bh"] = r;
                    dr0["mc"] = r;
                    tempTable.Rows.Add(dr0);
                }
                tempTable.AcceptChanges();
                CB_WZ.ItemsSource = tempTable.DefaultView;
                CB_WZ.SelectedIndex = 0;
                tempTable.Dispose();
            }
        }

        private void RQ3_Click(object sender, RoutedEventArgs e)
        {
            if (RQ3.IsChecked == true && CB3!=null)
            {
                CB1.IsEnabled = false;
                CB2.IsEnabled = false;
                CB3.IsEnabled = false;
                CB1.IsChecked = false;
                CB2.IsChecked = false;
                CB3.IsChecked = false;

                P.IsEnabled = true;
                P1.IsEnabled = true;
                P2.IsEnabled = true;
                P.IsChecked = false;
                P1.IsChecked = false;
                P2.IsChecked = false;
            }
        }

        private void RQ4_Click(object sender, RoutedEventArgs e)
        {
            if (RQ4.IsChecked == true && P2!=null)
            {
                CB1.IsEnabled = true;
                CB2.IsEnabled = true;
                CB3.IsEnabled = true;
                CB1.IsChecked = true;
                CB2.IsChecked = true;
                CB3.IsChecked = true;


                P.IsEnabled = false;
                P1.IsEnabled = false;
                P2.IsEnabled = false;
                P.IsChecked = false;
                P1.IsChecked = false;
                P2.IsChecked = false;
            }
        }
        #endregion

        #region 多选下拉框使用
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private Dictionary<string, object> _items;
        private Dictionary<string, object> _selectedItems;
        public Dictionary<string, object> Items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
                NotifyPropertyChanged("Items");
            }
        }
        public Dictionary<string, object> SelectedItems
        {
            get
            {
                return _selectedItems;
            }
            set
            {
                _selectedItems = value;
                NotifyPropertyChanged("SelectedItems");
            }
        }
        private void Submit()
        {
            foreach (KeyValuePair<string, object> s in SelectedItems)
                MessageBox.Show(s.Key);
        }
        #endregion

        private void Hour_Unchecked(object sender, RoutedEventArgs e)
        {
            if(RQ2.IsChecked==true)
            {
                bool uncheck = twoH.IsChecked!=true && eightH.IsChecked != true && DayH.IsChecked != true;
                if(uncheck)
                {
                    CheckBox cb = sender as CheckBox;
                    cb.IsChecked = true;
                }
            }
        }
    }
}
